<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="资产ID" prop="assetsNo">
            <el-input v-model="queryParams.assetsIdCard" placeholder="请输入资产ID" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-table v-loading="loading" :data="recordList" border>
      <el-table-column align="center" label="资产名称" prop="assetsName"/>
      <el-table-column align="center" label="资产ID" prop="assetsIdCard"/>
      <el-table-column align="center" label="购置单价" prop="bulMoney"/>
      <el-table-column align="center" label="购置日期" prop="bulTime">
        <template slot-scope="scope">
          <span>{{ scope.row.bulTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="维修基数" prop="maintenanceRv"/>
      <el-table-column align="center" label="是否已过折旧期" prop="isDepreciation">
        <template slot-scope="scope">
          <span>{{ scope.row.isDepreciation == 1 ? '是' : (scope.row.isDepreciation == 2 ? '否' : '是') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button  size="mini" type="text" @click="handleUpdate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total" @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="openDetails" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="form.assetsName" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产型号" prop="assetsModel">
          <el-input v-model="form.assetsModel" placeholder="请输入" disabled="disabled" />
        </el-form-item>
        <el-form-item label="资产ID" prop="assetsIdCard">
          <el-input v-model="form.assetsIdCard" placeholder="请输入" disabled="disabled">
            <template slot="prepend">{{ form.assetsNo }}-</template>
          </el-input>
        </el-form-item>
        <el-form-item label="购置日期" prop="bulTime">
          <el-date-picker clearable v-model="form.bulTime" type="date" value-format="yyyy-MM-dd" placeholder="年/月/日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修基数" prop="maintenanceRv">
          <el-input v-model="form.maintenanceRv" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="4" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDetailsForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getDetails, listDetails, updateDetails} from "@/api/cld/details";
import {getAssets} from "@/api/cld/assets";

export default {
  name: "Details",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 资产记录表格数据
      recordList: [],
      // 投放记录
      deliveryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetsIdCard: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        assetsIdCard: [
          { required: true, message: "资产ID不能为空", trigger: "blur" }
        ],
        bulTime: [
          { required: true, message: "购置日期不能为空", trigger: "blur" }
        ],
      },
      openDetails:false,
    };
  },
  created() {
    this.queryParams.assetsId = this.$route.params && this.$route.params.assetsId;
    this.getList();
  },
  methods: {
    /** 查询资产记录列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openDetails = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form1 = {
        assetsId: null,
        assetsNo: null,
        assetsName: null,
        assetsIdCard: null,
        bulMoney: null,
        bulTime: null,
        maintenanceRv: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 编辑资产明细 */
    handleUpdate(row){
      this.reset();
      const assetsId = row.assetsId || this.ids
      this.form.assetsId = assetsId;
      this.form.assetsName = row.assetsName || '';
      this.form.assetsNo = row.assetsNo || '';
      this.form.assetsModel = row.assetsModel || '';
      const assetsDetailsId = row.assetsDetailsId;
      getDetails(assetsDetailsId).then(response => {
        this.form = response.data;
        this.form.assetsIdCard = this.form.assetsIdCard.slice(this.form.assetsIdCard.lastIndexOf('-')+1);
        this.openDetails = true;
        this.title = "修改设备明细";
      });
    },
    /** 提交按钮 */
    submitDetailsForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateDetails(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openDetails = false;
            this.getList();
          });
        }
      });
    },
  }
};
</script>
